# -*- encoding=utf8 -*-
__author__ = "yupf"

from airtest.core.api import *

auto_setup(__file__)


def select_from_to_city(poco, from_city_code, to_city_code):
    # 订飞机票
    #     touch(Template(r"tpl1685282796139.png", record_pos=(0.015, -0.159), resolution=(1242, 2208)))
    touch(Template(r"tpl1686454649870.png", record_pos=(-0.2, 0.84), resolution=(1242, 2208)))

    log("选择 one way")
    touch(Template(r"tpl1685282821814.png", record_pos=(0.236, -0.604), resolution=(1242, 2208)))

    # 点击出发地选择框

    poco("Select departure airport").click()

    # touch(Template(r"tpl1685282929817.png", record_pos=(-0.06, -0.663), resolution=(1242, 2208)))
    touch(Template(r"tpl1685285181270.png", record_pos=(-0.437, -0.667), resolution=(1242, 2208)))
    #     poco('Choose Departure Airport').sibling('Other').offspring(nameMatches='Search by city.*$').click()

    text(from_city_code)

    poco(from_city_code).click()
    while True:
        if exists(Template(r"tpl1687079763087.png", record_pos=(0.004, -0.791), resolution=(1242, 2208))):
            swipe((0.5, 0.5), (0.5, 0.3))
            if poco(from_city_code).exists():
                poco(from_city_code).click()
            else:
                swipe((0.5, 0.5), (0.5, 0.3))
        else:
            break

    # 选择目的地

    poco("select_to_airport_button", type='Button').click()
    assert_exists(Template(r"tpl1685285446689.png", record_pos=(-0.006, -0.79), resolution=(1242, 2208)),
                  "进入目的地选择页面")

    touch(Template(r"tpl1685285181270.png", record_pos=(-0.437, -0.667), resolution=(1242, 2208)))
    #     poco('Choose Destination Airport').sibling('Other').offspring(nameMatches='Search by city.*$').click()

    text(to_city_code)
    poco(to_city_code).click()
    while True:
        if exists(Template(r"tpl1685285446689.png", record_pos=(-0.006, -0.79), resolution=(1242, 2208))):
            swipe((0.5, 0.5), (0.5, 0.3))
            if poco(to_city_code).exists():
                poco(to_city_code).click()
            else:
                swipe((0.5, 0.5), (0.5, 0.3))

        else:
            break

    wait(Template(r"tpl1685285795769.png", record_pos=(0.003, 0.28), resolution=(1242, 2208)))
